﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Web_Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private int kiemtra_role()
        {
            string sql = "SELECT ID_Role_User FROM DMS_User WHERE Name_User='" + txt_user.Text + "'";
            SqlConnection con = new SqlConnection(@"Data Source=BU\SQLEXPRESS;Initial Catalog=DMS_DEMO;Integrated Security=True");
            con.Open();
            SqlCommand com = new SqlCommand(sql,con);
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            int state = 1;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["ID_Role_User"].ToString() == 1 + "")
                    state = 1;
                else state = 2;

            }
            return state;   
        }
        private int kiemtra_user()
        {
            SqlConnection con = new SqlConnection(@"Data Source=BU\SQLEXPRESS;Initial Catalog=DMS_DEMO;Integrated Security=True");
            con.Open();
            string sql = "SELECT * FROM DMS_User WHERE Name_User = '" + txt_user.Text + "'AND Pass_User = '" + txt_pass.Text + "'";
            
            SqlCommand com = new SqlCommand(sql, con);
            //SqlDataAdapter sda = new SqlDataAdapter(com);
            //sda.Fill(ds);
            //DataSet ds = new DataSet();
            //int i = ds.Tables[0].Rows.Count;
            if (com.ExecuteReader().HasRows)
            {
                return 1;
            }
            return 0;
        }
        protected void btn_Login_Click(object sender, EventArgs e)
        {
            if (kiemtra_user()== 1)
            {
                if (CheckBox1.Checked)
                {
                    // Khởi tạo cookie
                    HttpCookie cookie_user = new HttpCookie("user", txt_user.Text);
                    HttpCookie cookie_pass = new HttpCookie("pass", txt_pass.Text);
                    // Gán thời gian sống của Cookie là 30 ngày 
                    cookie_user.Expires = DateTime.Now.AddDays(30);
                    cookie_pass.Expires = DateTime.Now.AddDays(30);
                    // Thêm Cookie 
                    Response.Cookies.Add(cookie_user);
                    Response.Cookies.Add(cookie_pass);

                    if (kiemtra_role() == 1)
                    {
                        //Response.Write(Request.Cookies["user"].Value);
                    }
                    else
                    {
                        //Response.Write(Request.Cookies["user"].Value);
                    }
                }
                else
                {
                    if (kiemtra_role() == 1)
                    {
                        Response.Write("Chào " + txt_user.Text);
                    }
                    else
                    {
                        Response.Write("Chào " + txt_user.Text);
                    }
                }
               
            }
            else Response.Write("Sai pass rui may!!!");
        }

        protected void btn_DeleteCookie_Click(object sender, EventArgs e)
        {
            // Khởi tạo cookie
            HttpCookie cookie_user = new HttpCookie("user", txt_user.Text);
            HttpCookie cookie_pass = new HttpCookie("pass", txt_pass.Text);
            // Gán thời gian sống của Cookie là 30 ngày 
            cookie_user.Expires = DateTime.Now;
            cookie_pass.Expires = DateTime.Now;
            // Thêm Cookie 
            Response.Cookies.Add(cookie_user);
            Response.Cookies.Add(cookie_pass);
            // làm tươi
            Response.Redirect("WebForm1.aspx");
        }
    }
}